/************************************************************************
	   Purpose: 	Collect summary statistics of bid prices and quantities
   *************************************************************************/

set more off
clear all
pause on

use "$TRADING_DATA_CLEAN/panel_plant-period-date-bid.dta", clear
drop if bid_status == "REJ"
drop if missing(bid_qty)

gen bid_qty_abs = abs(bid_qty)
gen bid_qty_purchase_abs = .
replace bid_qty_purchase_abs = abs(bid_qty) if bid_qty > 0
gen bid_qty_sale_abs = .
replace bid_qty_sale_abs = abs(bid_qty) if bid_qty < 0

gen trade_qty_abs = abs(trade_qty)
gen trade_qty_purchase_abs = .
replace trade_qty_purchase_abs = abs(trade_qty) if trade_qty > 0
gen trade_qty_sale_abs = .
replace trade_qty_sale_abs = abs(trade_qty) if trade_qty < 0

gen bid_price_purchase = .
replace bid_price_purchase = bid_price if bid_qty > 0
gen bid_price_sale = .
replace bid_price_sale = bid_price if bid_qty < 0

gen trade_price_purchase = .
replace trade_price_purchase = trade_price if trade_qty > 0
gen trade_price_sale = .
replace trade_price_sale = trade_price if trade_qty < 0

sum bid_qty_*abs trade_qty_*abs bid_price* trade_price*

tempfile cleaned_data
save `cleaned_data'

matrix data = J(14, 3, 0)

********************************************************************************
*** Version 1, Panel A: Bids
********************************************************************************

** Unweighted
local counter = 1
foreach var of varlist bid_qty_*abs {
	sum `var'
	matrix data[1, `counter'] = r(mean)
	matrix data[2, `counter'] = r(sd)
	local counter = `counter' + 1
}

local counter = 1
foreach var of varlist bid_price*{
	sum `var'
	matrix data[3, `counter'] = r(mean)
	matrix data[4, `counter'] = r(sd)
	local counter = `counter' + 1
}

** Weighted
sum bid_price [aweight=bid_qty_abs]
matrix data[5, 1] = r(mean)
matrix data[6, 1] = r(sd)
matrix data[7, 1] = r(N)

local counter = `counter' + 1
sum bid_price_purchase [aweight=bid_qty_purchase_abs]
matrix data[5, 2] = r(mean)
matrix data[6, 2] = r(sd)
matrix data[7, 2] = r(N)

local counter = `counter' + 1
sum bid_price_sale [aweight=bid_qty_sale_abs]
matrix data[5, 3] = r(mean)
matrix data[6, 3] = r(sd)
matrix data[7, 3] = r(N)

********************************************************************************
*** Version 1, Panel B: Trade
********************************************************************************

** Unweighted
local counter = 1
foreach var of varlist trade_qty_*abs {
	sum `var'
	matrix data[8, `counter'] = r(mean)
	matrix data[9, `counter'] = r(sd)
	local counter = `counter' + 1
}

local counter = 1
foreach var of varlist trade_price* {
	sum `var'
	matrix data[10, `counter'] = r(mean)
	matrix data[11, `counter'] = r(sd)
	local counter = `counter' + 1
}

** Weighted
local counter = `counter' + 1
sum trade_price [aweight=trade_qty_abs]
matrix data[12, 1] = r(mean)
matrix data[13, 1] = r(sd)
matrix data[14, 1] = r(N)

local counter = `counter' + 1
sum trade_price_purchase [aweight=trade_qty_purchase_abs]
matrix data[12, 2] = r(mean)
matrix data[13, 2] = r(sd)
matrix data[14, 2] = r(N)

local counter = `counter' + 1
sum trade_price_sale [aweight=trade_qty_sale_abs]
matrix data[12, 3] = r(mean)
matrix data[13, 3] = r(sd)
matrix data[14, 3] = r(N)

matrix colnames data = ///
	"All" ///
	"Purchase" ///
	"Sale"
	
*** Output
clear 
svmat double data, names(col)

foreach table_panel in "A" "B" {
	preserve
	* Select observations
	if "`table_panel'" == "A" {
		drop if _n > 7
	}
	if "`table_panel'" == "B" {
		drop if _n < 8
	}

	* Cleanup vars
	foreach v of varlist * {
	gen `v'_str = string(round(`v', 0.01), "%9.2f")
	replace `v'_str = string(`v', "%9.0f") if mod(_n, 7) == 0
	replace `v'_str = "(" + `v'_str + ")"  if mod(_n, 2) == 0 & _n != 7
	replace `v'_str = "\multicolumn{1}{c}{" + `v'_str + "}"
	drop `v'
	rename `v'_str `v'
}

	* Set varnames
	local rname_type = "Order"
	if "`table_panel'" == "B" local rname_type = "Trade"

	gen rname = "", before("All")
	replace rname = "`rname_type' quantity (kg)" if _n == 1
	replace rname = "`rname_type' price (Rs/kg)" if _n == 3
	replace rname = "`rname_type' price (Rs/kg), weighted by quantity" if _n == 5
	replace rname = "Observations" if _n == 7

	* Add empty row
	qui: ds 
	gen last = _n == _N
	expand 2 if last, gen(dup)
	foreach var in `r(varlist)' {
		replace `var' = "\multicolumn{1}{c}{}" if dup == 0 & last == 1
	}
	drop last dup

	listtex using "$TRADING_TABS/Table_B1_`table_panel'.tex", rstyle(tabular) replace
	restore
}




********************************************************************************
*** Version 2, Detail
********************************************************************************
use `cleaned_data', clear

bysort gpcb_id period: gen num_bids = _N
bysort gpcb_id: gen num_bids_total = _N

matrix data = J(33, 9, 0)
forvalues period = 1/10 {
	* Fill period
	matrix data[3*`period'-2, 1] = `period'
	matrix data[3*`period'-1, 1] = .
	matrix data[3*`period', 1] = .
	
	matrix data[3*`period'-2, 2] = .
	matrix data[3*`period'-1, 2] = .
	matrix data[3*`period', 2] = .
	
	sum num_bids if period == `period'
	matrix data[3*`period' - 2, 2] = r(N)
	local counter = 3
	foreach var of varlist num_bids bid_qty_abs bid_qty_purchase_abs bid_qty_sale_abs bid_price bid_price_purchase bid_price_sale {
		sum `var' if period == `period'
		if ("`var'" == "num_bids"){
			preserve
			keep if period == `period'
			keep gpcb_id commodity num_bids
			merge m:1 gpcb_id commodity using "$TRADING_DATA_OUT/inventory_plant-commodity.dta", keepusing(commodity_period)
			replace num_bids = 0 if missing(num_bids)
			keep if commodity_period == `period'
			duplicates drop
			sum `var'
			matrix data[3*`period'-2, `counter'] = round(r(mean), 0.01)
			matrix data[3*`period'-1, `counter'] = round(r(sd), 0.01)
			matrix data[3*`period', `counter'] = .
			restore
		} 
		else {
			sum `var' if period == `period'
			matrix data[3*`period'-2, `counter'] = round(r(mean), 0.01)
			matrix data[3*`period'-1, `counter'] = round(r(sd), 0.01)
			matrix data[3*`period', `counter'] = .
		}

		local counter = `counter' + 1
	}
}


local period = 11
local counter = 3
matrix data[3*`period'-2, 1] = `period'
matrix data[3*`period'-1, 1] = .
matrix data[3*`period', 1] = .

sum num_bids_total
matrix data[3*`period' - 2, 2] = r(N)
matrix data[3*`period'-1, 2] = .
matrix data[3*`period', 2] = .
local counter = 3
foreach var of varlist num_bids_total bid_qty_abs bid_qty_purchase_abs bid_qty_sale_abs bid_price bid_price_purchase bid_price_sale {
	if "`var'" == "num_bids_total" {
		preserve
		keep gpcb_id num_bids_total
		duplicates drop
		merge 1:1 gpcb_id using "$TRADING_DATA_OUT/index_plant.dta"
		replace num_bids_total = 0 if missing(num_bids_total) 
		sum `var'
		matrix data[3*`period'-2, `counter'] = round(r(mean), 0.01)
		matrix data[3*`period'-1, `counter'] = round(r(sd), 0.01)
		matrix data[3*`period', `counter'] = .		
		restore
	} 
	else {
		sum `var'
		matrix data[3*`period'-2, `counter'] = round(r(mean), 0.01)
		matrix data[3*`period'-1, `counter'] = round(r(sd), 0.01)
		matrix data[3*`period', `counter'] = .
		}
	
	local counter = `counter' + 1
}

	
**** Output
clear 
svmat double data, names(col)

* Cleanup vars
foreach v of varlist * {
	if inlist("`v'", "c7", "c8", "c9") {
		gen `v'_str = string(round(`v', 0.01), "%9.2f")
	}
	else if "`v'" == "c3" {
		gen `v'_str = string(round(`v', 0.1), "%9.1f")
	}
	else {
		gen `v'_str = string(`v', "%9.0fc")
	}
	replace `v'_str = "" if `v'_str == "."
	replace `v'_str = "(" + `v'_str + ")" if mod(_n, 3) == 2 & `v'_str != ""
	replace `v'_str = "Total" if _n == 31 & "`v'" == "c1"
	drop `v'
	rename `v'_str `v'
}
drop if _n == _N
listtex using "$TRADING_TABS/Table_2.tex", rstyle(tabular) replace ///
headlines("\begin{tabular}{c*{8}{c}} \toprule &  & & \multicolumn{3}{c}{Bid quantities (kg)} & \multicolumn{3}{c}{Bid prices (INR/kg)} \\ \cmidrule(lr){4-6} \cmidrule(lr){7-9} \multicolumn{1}{c}{Period} & \multicolumn{1}{c}{Count} & \multicolumn{1}{c}{Bids per plant} & \multicolumn{1}{c}{All bids} & \multicolumn{1}{c}{Buy bids} & \multicolumn{1}{c}{Sell bids} & \multicolumn{1}{c}{All bids} & \multicolumn{1}{c}{Buy bids} & \multicolumn{1}{c}{Sell bids} \\ & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} & \multicolumn{1}{c}{(4)} & \multicolumn{1}{c}{(5)} & \multicolumn{1}{c}{(6)} & \multicolumn{1}{c}{(7)} & \multicolumn{1}{c}{(8)} \\ \midrule") ///
footlines("\addlinespace \bottomrule \end{tabular}")
